﻿using System.Collections.Generic;
using System.Linq;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Controller
{
    public class CommentController
    {
        public IList<Comment> GetAllComment()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Comment>().ToList();
            }
        }

        public void AddComment(Comment comment)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(comment);
                    transaction.Commit();
                }
            }
        }

        public void EditComment(Comment newComment)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(newComment);
                    transaction.Commit();
                }
            }
        }

        public void DeleteComment(Comment comment)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(comment);
                    transaction.Commit();
                }
            }
        }
    }
}
